package com.negative.logic;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.negative.activities.R;
import com.negative.data.PurchaseData;
import com.negative.utils.SharingManager;

public class PurchaseListAdapter extends ArrayAdapter<PurchaseData> {

	Context context;
	ArrayList<PurchaseData> purchases;
	private LayoutInflater vi;
	private boolean withFacebook;

	public PurchaseListAdapter(Context context,
			ArrayList<PurchaseData> purchases, boolean withFacebook) {
		super(context, 0, purchases);

		this.context = context;
		this.purchases = purchases;
		this.withFacebook = withFacebook;
		vi = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final PurchaseData purchase = purchases.get(position);
		if (purchase != null) {

			v = vi.inflate(R.layout.purchase_list_entry, null);

			final TextView eventName = (TextView) v
					.findViewById(R.id.purchase_list_event_name);
			final TextView eventStartDate = (TextView) v
					.findViewById(R.id.purchase_list_event_start_date);
			// final ImageView calendar = (ImageView)
			// v.findViewById(R.id.purchase_list_entry_calendar);

			eventName.setText(purchase.getEventName());
			eventStartDate.setText(context
					.getString(R.string.purchase_list_event_start_date)
					+ purchase.getEventStartDate());

			final ImageView facebookIcon = (ImageView) v
					.findViewById(R.id.purchase_list_facebook_icon);
			facebookIcon.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					SharingManager.shareToFacebook(purchase.getEventName());
				}
			});
			
			if (withFacebook == false) {
//				facebookIcon.setVisibility(ImageView.INVISIBLE);
				Bitmap original = BitmapFactory.decodeResource(context.getResources(), R.drawable.facebook_icon);
				Bitmap grayImage = grayScaleImage(original);
				facebookIcon.setImageBitmap(grayImage);
				facebookIcon.setEnabled(false);
			}

			// calendar.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// addEventToCalendar(eventName, eventStartDate);
			// }
			// });
			v.setTag(purchase);
		}
		return v;
	}

	private void addEventToCalendar(String title, long startDate) {
		Calendar cal = Calendar.getInstance();
		Intent intent = new Intent(Intent.ACTION_EDIT);
		intent.setType("vnd.android.cursor.item/event");
		intent.putExtra("beginTime", startDate);
		intent.putExtra("allDay", false);
		intent.putExtra("endTime", startDate + (3 * 60 * 60 * 1000)); // 3*60*60*1000
																		// =
																		// 3HRs
																		// -
																		// Approximate
																		// movie
																		// length
		intent.putExtra("title", title);
		intent.putExtra("eventLocation",
				context.getString(R.string.negative_theater));
		context.startActivity(intent);
	}
	
	public static Bitmap grayScaleImage(Bitmap src) {
		  // constant factors
		  final double GS_RED = 0.299;
		  final double GS_GREEN = 0.587;
		  final double GS_BLUE = 0.114;
		 
		  // create output bitmap
		  Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
		  // pixel information
		  int A, R, G, B;
		  int pixel;
		 
		  // get image size
		  int width = src.getWidth();
		  int height = src.getHeight();
		 
		  // scan through every single pixel
		  for(int x = 0; x < width; ++x) {
		   for(int y = 0; y < height; ++y) {
		    // get one pixel color
		    pixel = src.getPixel(x, y);
		    // retrieve color of all channels
		    A = Color.alpha(pixel);
		    R = Color.red(pixel);
		    G = Color.green(pixel);
		    B = Color.blue(pixel);
		    // take conversion up to one single value
		    R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
		    // set new pixel color to output bitmap
		    bmOut.setPixel(x, y, Color.argb(A, R, G, B));
		   }
		  }
		 
		  // return final image
		  return bmOut;
		 }
}
